package com.zzyl.controller.security;

import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 资源列表
     *
     * @param dto 资源Dto
     * @return {@code ResponseResult<List<ResourceVo>> }
     * @author YiSheep
     */
    @ApiOperation("资源列表")
    @PostMapping("/list")
    public ResponseResult<List<ResourceVo>> resourceList(@RequestBody ResourceDto dto){
        log.info("资源列表");
        List<ResourceVo> resourceVos = resourceService.findResourceList(dto);
        return ResponseResult.success(resourceVos);
    }
    /**
     * 资源树形
     *
     * @param dto 资源Dto
     * @return {@code ResponseResult<TreeVo> }
     * @author YiSheep
     */
    @ApiOperation(value = "资源树形", notes = "资源树形")
    @PostMapping("/tree")
    public ResponseResult<TreeVo> resourceTree(@RequestBody ResourceDto dto) {
        log.info("资源树形");
        TreeVo treeVo = resourceService.resourceTree(dto);
        return ResponseResult.success(treeVo);
    }

    /**
     * 资源添加
     *
     * @param dto 资源Dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("资源添加")
    @PutMapping
    public ResponseResult<Void> resourceAdd(@RequestBody ResourceDto dto) {
        log.info("资源添加:{}", dto);
        resourceService.resourceAdd(dto);
        return ResponseResult.success();
    }

    /**
     * 资源修改
     *
     * @param dto 资源Dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("资源修改")
    @PatchMapping
    public ResponseResult<Void> resourceUpdate(@RequestBody ResourceDto dto) {
        log.info("资源修改:{}", dto);
        resourceService.resourceUpdate(dto);
        return ResponseResult.success();
    }
    /**
     * 启用禁用
     *
     * @param dto 资源Dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("启用禁用")
    @PostMapping("/enable")
    public ResponseResult<Void> enableOrDisable(@RequestBody ResourceDto dto) {
        log.info("启用禁用");
        resourceService.enableOrDisable(dto);
        return ResponseResult.success();
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单id
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("删除菜单")
    @DeleteMapping("/{menuId}")
    public ResponseResult<Void> resourceDelete(@PathVariable Long menuId) {
        log.info("删除菜单:{}", menuId);
        resourceService.resourceDelete(menuId);
        return ResponseResult.success();
    }

    ////////////////////////////////////////////////////////////////////////////////////

    @GetMapping("/menus")
    @ApiOperation("我的菜单")
    public ResponseResult<List<MenuVo>> getMyMenus() {
        log.info("我的菜单");
        List<MenuVo> menus = resourceService.getMyMenus();
        return ResponseResult.success(menus);
    }
}
